//
// Created by DMC on 2024/5/11.
//

// 抽象工厂模式
#include <iostream>

// 图形类
class Shape {
public:
    virtual void draw() = 0;
};

class Rectangle : public Shape {
public:
    void draw() override {
        std::cout << "Rectangle::draw()" << std::endl;
    }
};

class Circle : public Shape {
public:
    void draw() override {
        std::cout << "Circle::draw()" << std::endl;
    }
};

// 颜色类
class Color {
public:
    virtual void fill() = 0;
};

class Red : public Color {
public:
    void fill() override {
        std::cout << "Red::fill()" << std::endl;
    }
};

class Green : public Color {
public:
    void fill() override {
        std::cout << "Green::fill()" << std::endl;
    }
};

// 抽象工厂类
class AbstractFactory {
public:
    virtual Shape* getShape(const std::string &shape) = 0;
    virtual Color* getColor(const std::string &color) = 0;
};

class ShapeFactory : public AbstractFactory {
public:
    Shape* getShape(const std::string &shape) override {
        if (shape == "Rectangle") {
            return new Rectangle();
        } else if (shape == "Circle") {
            return new Circle();
        }
        return nullptr;
    }

    Color* getColor(const std::string &color) override {
        return nullptr;
    }
};

class ColorFactory : public AbstractFactory {
public:
    Shape* getShape(const std::string &shape) override {
        return nullptr;
    }

    Color* getColor(const std::string &color) override {
        if (color == "Red") {
            return new Red();
        } else if (color == "Green") {
            return new Green();
        }
        return nullptr;
    }
};

// 对象工厂类
class ObjectFactory {
public:
    static AbstractFactory* getFactory(const std::string &factory) {
        if (factory == "Shape") {
            return new ShapeFactory();
        } else if (factory == "Color") {
            return new ColorFactory();
        }
        return nullptr;
    }
};


